package cc.vv.baselibrary.view.imgselector.album;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;

import cc.vv.baselibrary.R;
import cc.vv.baselibrary.view.BaseTopBarView;
import cc.vv.baselibrary.view.imgselector.AlbumConfig;
import cc.vv.baselibrary.view.imgselector.ImageSelector;
import cc.vv.baselibrary.view.imgselector.album.previewimage.ImageDetailFragment;
import cc.vv.baselibrary.view.imgselector.base.ImageSelectorActivity;
import cc.vv.baselibrary.view.imgselector.model.AlbumRepository;
import cc.vv.baselibrary.view.imgselector.utils.ActivityUtils;
import cc.vv.baselibrary.view.jzvd.JZVideoPlayer;


public class AlbumActivity extends ImageSelectorActivity implements AlbumContract.ChooseFolderName {

    public static final String TAG = AlbumActivity.class.getSimpleName();

    private AlbumPresenterImage mAlbumPresenter;

    private BaseTopBarView mSubmitBtn;

    private AlbumConfig mAlbumConfig;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.galleryselect_activity_album);
        mAlbumConfig = getIntent().getParcelableExtra(ImageSelector.ARG_ALBUM_CONFIG);
        initViews();
        AlbumFragment albumFragment;
        ImageDetailFragment imageDetailFragment;

        if (savedInstanceState != null) {
            //内存重启时调用   解决 内存重启时可能发生的Fragment重叠异常
            imageDetailFragment = (ImageDetailFragment) getSupportFragmentManager()
                    .findFragmentByTag(ImageDetailFragment.TAG);

            albumFragment = (AlbumFragment) getSupportFragmentManager().findFragmentByTag(AlbumFragment.TAG);
            albumFragment.setFolderNameInterface(this);
            if (imageDetailFragment != null && albumFragment != null) {
                getSupportFragmentManager().beginTransaction()
                        .hide(albumFragment)
                        .show(imageDetailFragment)
                        .commit();
            }

        } else {
            //创建AlbumFragment
            albumFragment = AlbumFragment.newInstance();
            albumFragment.setFolderNameInterface(this);
            ActivityUtils.addFragmentToActivity(
                    getSupportFragmentManager(), albumFragment, AlbumFragment.TAG, false);
        }
        AlbumRepository albumRepository = AlbumRepository.getInstance(this, 0);
        albumRepository.setFilterSmallPic(mAlbumConfig.isMfilterSmallPic());
        albumRepository.setShowVideo(mAlbumConfig.isShowVideo());
        //创建AlbumPresenter
        mAlbumPresenter = new AlbumPresenterImage(
                albumRepository,
                getSupportLoaderManager(),
                albumFragment, mAlbumConfig.isShowVideo());
    }


    private void initViews() {
        mSubmitBtn = findViewById(R.id.jtb_alb_top_bar);
        if (mAlbumConfig.isShowVideo()) {
            mSubmitBtn.setTitle("图片和视频");
        } else {
            mSubmitBtn.setTitle("所有图片");
        }
        mSubmitBtn.setRightTVColor(ContextCompat.getColor(this, R.color.color_28BFA9));
        mSubmitBtn.setEventInterface(new BaseTopBarView.EventInterface() {
            @Override
            public void leftOnClick() {
                onBackPressed();

            }

            @Override
            public void leftCloseClick() {

            }

            @Override
            public void rightOnClick() {
                mAlbumPresenter.returnResult();
            }
        });

    }

    public void setSubmitBtnText(CharSequence text, boolean enabled) {
        mSubmitBtn.setRightTVContent(text.toString());
        mSubmitBtn.setEnabled(enabled);
    }

    public void setToolbarTitle(CharSequence title) {

    }

    @Override
    public void onBackPressed() {
        AlbumFragment albumFragment =
                (AlbumFragment) getSupportFragmentManager().findFragmentByTag(AlbumFragment.TAG);

        if (albumFragment == null) {
            super.onBackPressed();
            return;
        }

        if (albumFragment.isHidden()) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .show(albumFragment)
                    .commit();
        } else if (albumFragment.mFab.getVisibility() != View.VISIBLE) {
            albumFragment.hideFolderList();
            return;
        }
        super.onBackPressed();
    }


    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAlbumPresenter.clearCache();
        JZVideoPlayer.releaseAllVideos();
    }

    @Override
    public void onConfigurationChanged(Configuration config) {
        super.onConfigurationChanged(config);
    }

    @Override
    public void folderName(String name) {
        if (TextUtils.isEmpty(name)) {
            mSubmitBtn.setTitle("图片");
        } else {
            mSubmitBtn.setTitle(name);
        }
    }
}
